package com.example.service.impl;

import com.example.constant.SeatTypeEnum;
import com.example.mapper.EventMapper;
import com.example.mapper.SeatMapper;
import com.example.mapper.TicketMapper;
import com.example.pojo.Event;
import com.example.pojo.Seat;
import com.example.pojo.Ticket;
import com.example.service.SeatService;
import com.example.vo.SeatPurchasedVO;
import com.example.vo.SeatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SeatServiceImpl implements SeatService {

    @Autowired
    private SeatMapper seatMapper;
    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private EventMapper eventMapper;

    @Override
    public void save() {
        List<Seat> seatList = new ArrayList<>();
        for (int i = 1; i <= 600; i++) {
            Seat seat = new Seat();
            //座位号
            if (i <= 300) {
                seat.setSeatNum(i);
                //场馆
                seat.setPlace((short) 1);
                if (i <= 100) {
                    seat.setSeatType((short) 1);
                } else if (i <= 200) {
                    seat.setSeatType((short) 2);
                } else {
                    seat.setSeatType((short) 3);
                }
            } else {
                seat.setSeatNum(i - 300);
                seat.setPlace((short) 2);
                if (i <= 400) {
                    seat.setSeatType((short) 1);
                } else if (i <= 500) {
                    seat.setSeatType((short) 2);
                } else {
                    seat.setSeatType((short) 3);
                }
            }

            //座位号越低，证明越内圈

            seatList.add(seat);
        }
        seatMapper.save(seatList);
    }

    @Override
    public List<Seat> list(Short place, Short seatType) {
        List<Seat> seatList = seatMapper.list(place,seatType);
        return seatList;
    }

    @Override
    public SeatPurchasedVO seatPurchased(Integer eventID) {
        Event event = eventMapper.getByID(eventID);
        Short place = event.getPlace();
        //指定赛事已经购买的票
        List<Ticket> ticketList = ticketMapper.list(null,eventID,null);
        //所有的座位表
        List<Seat> seatList = seatMapper.list(place, null);
        // 座位分类
        List<Seat> lowSeat = seatList.stream().filter(seat -> Objects.equals(seat.getSeatType(), SeatTypeEnum.LOW_SEAT.getCode()))
                .collect(Collectors.toList());
        List<Seat> midSeat = seatList.stream().filter(seat -> Objects.equals(seat.getSeatType(), SeatTypeEnum.MID_SEAT.getCode()))
                .collect(Collectors.toList());
        List<Seat> highSeat = seatList.stream().filter(seat -> Objects.equals(seat.getSeatType(), SeatTypeEnum.HIGH_SEAT.getCode()))
                .collect(Collectors.toList());
        //需要给前端返回的集合一共两个属性，一个seatID,另一个是否被购买true表示已经被购买
        SeatPurchasedVO seatPurchasedVOList = new SeatPurchasedVO();
        List<SeatVO> lowSeatVoLi = new ArrayList<>();
        List<SeatVO> midSeatVoLi = new ArrayList<>();
        List<SeatVO> highSeatVoLi = new ArrayList<>();
        // 抵挡票封装
        lowSeat.forEach(seat -> {
            SeatVO seatVO = new SeatVO();
            seatVO.setId(seat.getId()).setSeatNum(seat.getSeatNum());
            long count = ticketList.stream().filter(ticket -> ticket.getSeatID() == seat.getId()).count();
            seatVO.setBePurchased(count != 0);
            lowSeatVoLi.add(seatVO);
        });
        seatPurchasedVOList.setLowPriceSeats(lowSeatVoLi);
        // 中档票封装
        midSeat.forEach(seat -> {
            SeatVO seatVO = new SeatVO();
            seatVO.setId(seat.getId()).setSeatNum(seat.getSeatNum());
            long count = ticketList.stream().filter(ticket -> ticket.getSeatID() == seat.getId()).count();
            seatVO.setBePurchased(count != 0);
            midSeatVoLi.add(seatVO);
        });
        seatPurchasedVOList.setMidPriceSeats(midSeatVoLi);
        // 高档票封装
        highSeat.forEach(seat -> {
            SeatVO seatVO = new SeatVO();
            seatVO.setId(seat.getId()).setSeatNum(seat.getSeatNum());
            long count = ticketList.stream().filter(ticket -> ticket.getSeatID() == seat.getId()).count();
            seatVO.setBePurchased(count != 0);
            highSeatVoLi.add(seatVO);
        });
        seatPurchasedVOList.setHighPriceSeats(highSeatVoLi);
        return seatPurchasedVOList;
    }

}
